<?php
/**
-------------------------
GNU GPL COPYRIGHT NOTICES
-------------------------
This file is part of FlexicaCMS.

FlexicaCMS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FlexicaCMS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FlexicaCMS.  If not, see <http://www.gnu.org/licenses/>.*/

/**
 * $Id$
 *
 * @author FlexicaCMS team <contact@flexicacms.com>
 * @link http://www.flexicacms.com/
 * @copyright Copyright &copy; 2009-2010 Gia Han Online Solutions Ltd.
 * @license http://www.flexicacms.com/license.html
 */
class AliasInput extends CInputWidget
{
    /**
     * @var string column name of the title field
     */
    public $titleField = 'title';

    public function run()
    {
        if ($this->model) {
            $class = get_class($this->model);
            echo CHtml::activeTextField($this->model, $this->attribute, $this->htmlOptions);
        } else {
            $class = '';
            $this->attribute = $this->name;
            echo CHtml::textField($this->attribute, $this->value, $this->htmlOptions);
        }

        $script = "
        var title = $('#" . $class . "_" . $this->titleField . "');
        title.click(function(){
            title.data('value', title.val());
        });

        title.blur(function(){
            var text = $(this).val();
            if (text == title.data('value')) return;
            var alias = text
                    .toLowerCase() // change everything to lowercase
                    .replace(/^\s+|\s+$/g, '') // trim leading and trailing spaces        
                    .replace(/[_|\s]+/g, '-') // change all spaces and underscores to a hyphen
                    .replace(/[^a-z0-9-]+/g, '') // remove all non-alphanumeric characters except the hyphen
                    .replace(/[-]+/g, '-') // replace multiple instances of the hyphen with a single instance
                    .replace(/^-+|-+$/g, '') // trim leading and trailing hyphens                
                    ;
            $('#" . $class . "_" . $this->attribute . "').val(alias);
        });
        ";

        cs()->registerScript('title-to-slug', $script, CClientScript::POS_READY);
    }
}

?>
